package tools

import (
	"bugManagementPlatform/variable"
	"gorm.io/gorm"
)

type Function func(tx *gorm.DB, params ...interface{}) (interface{}, error)

func TransactionDecorator(function Function, params ...interface{}) (interface{}, error){
	tx := variable.Db.Begin()

	defer func() {
		if err := recover(); err != nil {
			tx.Rollback()
		}
	}()

	res, err := function(tx, params...)
	if err != nil{
		tx.Rollback()
	}else {
		tx.Commit()
	}

	return res, err
}



